﻿using Infrastructure.Dao;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WsdShop.IService;
using WsdShop.Model.SearchModels.Activity;
using WsdShop.Model.ViewModels;

namespace WsdShop.Service
{
    public class ActivityTeam : IActivityTeam
    {
        private SqlSugarClient db;


        /// <summary>
        /// 根据一分钱活动团id获取团信息
        /// </summary>
        /// <param name="skip"></param>
        /// <param name="take"></param>
        /// <param name="activityid"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public V_ActivityTeam GetActivityTeamInfo(int id, ref int total)
        {
            using (db = DbConfig.GetInstance())
            {
                var list = db.Queryable<Model.ActivityTeam>()
                    .OrderBy(o => o.CreateTime, OrderByType.Desc).Where(m => m.Id == id)
                    .ToPageList(0 / 5 + 1, 5, ref total)
                    .Select(s => new V_ActivityTeam()
                    {
                        CoverImg = db.Queryable<Model.Activity>().Where(t => t.Id == s.ActivityId).FirstOrDefault().CoverImg,
                        ActivityId = s.ActivityId,
                        CreateTime = s.CreateTime,
                        ActivityName = s.ActivityName,
                        ActivityPrice = s.ActivityPrice,
                        ActivityTeamMembers = db.Queryable<Model.ActivityTeamMembers>().Where(t => t.ActivityTeamId == s.Id).ToList(),
                        Id = s.Id,
                        ContinuedTime = s.ContinuedTime,
                        IsDeleted = s.IsDeleted,
                        NowParticipateNum = s.NowParticipateNum,
                        ParticipateNum = s.ParticipateNum,
                        TeamNo = s.TeamNo,
                        TeamStatus = s.TeamStatus,
                        UserHeadImg = s.UserHeadImg,
                        UserId = s.UserId,
                        UserName = s.UserName,
                        EndTime = s.EndTime,
                        IsParticipateNum = db.Queryable<Model.Activity>().Where(t => t.Id == s.ActivityId).FirstOrDefault().ParticipateNum
                    }).ToList()[0];
                return list;
            }
        }


        /// <summary>
        /// 分页获取活动列表
        /// </summary>
        /// <param name="search"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public IEnumerable<V_ActivityTeam> GetActivityTeamList(ActivitySearchModel search, ref int total)
        {
            using (db = DbConfig.GetInstance())
            {
                db.CurrentFilterKey = "FalseDelte";
                search.OrderByFields = search.OrderByFields;

                var list = db.Queryable<Model.ActivityTeam>()
                    .Where(m => m.UserName.Contains(search.Keywords) && m.ActivityId == search.Id)
                    .OrderBy(search.OrderByFields).ToPageList(search.Skip / search.Take + 1, search.Take, ref total)
                    .Select(s => new V_ActivityTeam()
                    {
                        ActivityId = s.ActivityId,
                        ActivityName = s.ActivityName,
                        ActivityPrice = s.ActivityPrice,
                        ContinuedTime = s.ContinuedTime,
                        CreateTime = s.CreateTime,
                        EndTime = s.EndTime,
                        Id = s.Id,
                        IsDeleted = s.IsDeleted,
                        NowParticipateNum = s.NowParticipateNum,
                        ParticipateNum = s.ParticipateNum,
                        TeamNo = s.TeamNo,
                        TeamStatus = s.TeamStatus,
                        UserHeadImg = s.UserHeadImg,
                        UserId = s.UserId,
                        UserName = s.UserName
                    }).ToList();
                return list;
            }
        }

        /// <summary>
        /// 分页获取活动列表
        /// </summary>
        /// <param name="search"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public IEnumerable<V_ActivityTeamMembers> GetActivityTeamMembersList(ActivitySearchModel search, ref int total)
        {
            using (db = DbConfig.GetInstance())
            {
                db.CurrentFilterKey = "FalseDelte";
                search.OrderByFields = search.OrderByFields;

                var list = db.Queryable<Model.ActivityTeamMembers>()
                    .Where(m => m.UserName.Contains(search.Keywords) && m.ActivityTeamId == search.Id)
                    .OrderBy(search.OrderByFields).ToPageList(search.Skip / search.Take + 1, search.Take, ref total)
                    .Select(s => new V_ActivityTeamMembers()
                    {
                        ActivityId = s.ActivityId,
                        ActivityTeamId = s.ActivityTeamId,
                        DrawId = s.DrawId,
                        DrawRewardName = s.DrawRewardName,
                        Id = s.Id,
                        IsDeleted = s.IsDeleted,
                        IsHead = s.IsHead,
                        JoinTime = s.JoinTime,
                        OrderNo = s.OrderNo,
                        PaymentAmount = s.PaymentAmount,
                        RefundStatus = s.RefundStatus,
                        UserId = s.UserId,
                        UserName = s.UserName,
                        UserHeadImg = s.UserHeadImg,
                        WinningStatus = s.WinningStatus
                    }).ToList();
                return list;
            }
        }

        /// <summary>
        /// 修改团
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool EditActivityTeam(Model.ActivityTeam model)
        {
            using (db = DbConfig.GetInstance())
            {
                return db.Update<Model.ActivityTeam>(model);
            }
        }


    }
}
